package com.app.micaihu.view.army.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.bean.infor.AttentionStautsBean
import com.app.micaihu.bean.news.ShareBean
import com.app.micaihu.configure.LoadType
import com.app.micaihu.utils.ApiServiceUtils.apiService
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.army.contract.ArmyMemberContract
import com.app.micaihu.view.bean.ArmyCurrentMember
import com.app.micaihu.view.bean.ArmyGrade
import com.app.micaihu.view.bean.ArmyMember
import com.app.utils.biz.Biz

/**
 * 军团成员列表
 */
open class ArmyMemberPresenter(gid: String) : BasePresenter<ArmyMemberContract.View>(), ArmyMemberContract.Presenter {
    private var gid = ""

    init {
        this.gid = gid
    }

    private var page = 1
    private var firstLoad = true


    override fun armyMemberList() {
        if (firstLoad) {
            getView()?.showLoading()
            firstLoad = false
        }
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armyGroupMemberList(params), object : NetRequestListener<BaseBean<ArmyMember?>?> {
            override fun onSuccess(result: BaseBean<ArmyMember?>?) {
                getView()?.hideLoading()
                page++
                getView()?.handleArmyMemberList(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleArmyMemberList(null)
                getView()?.onError(e)
            }
        })
    }

    override fun follow(type: Int, toUid: String) {
        val params = ParamsMapUtils.commonParams
        params["uid"] = Biz.instance.uid()
        params["type"] = type
        params["toUid"] = toUid
        requestNetwork(apiService.follow(params), object : NetRequestListener<BaseBean<AttentionStautsBean?>?> {
            override fun onSuccess(result: BaseBean<AttentionStautsBean?>?) {
                getView()?.hideLoading()
                page++
                getView()?.handleFollow(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleFollow(null)
                getView()?.onError(e)
            }
        })
    }

    override fun deleteMember(gid: String, toUid: String) {
        getView()?.showLoading()
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["to_uid"] = toUid
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.deleteMember(params), object : NetRequestListener<BaseBean<Any?>?> {
            override fun onSuccess(result: BaseBean<Any?>?) {
                getView()?.hideLoading()
                getView()?.handleDeleteMember(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleDeleteMember(null)
                getView()?.onError(e)
            }
        })
    }

    override fun share() {
        val params = ParamsMapUtils.commonParams
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armyShare(params), object : NetRequestListener<BaseBean<ShareBean?>?> {
            override fun onSuccess(result: BaseBean<ShareBean?>?) {
                getView()?.hideLoading()
                getView()?.handleShare(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleShare(null)
                getView()?.onError(e)
            }
        })
    }

    override fun armyGradeSet() {
        val params = ParamsMapUtils.commonParams
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armyGradeSet(params), object : NetRequestListener<BaseBean<List<ArmyGrade>?>?> {
            override fun onSuccess(result: BaseBean<List<ArmyGrade>?>?) {
                getView()?.hideLoading()
                getView()?.handleArmyGradeSet(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleArmyGradeSet(null)
                getView()?.onError(e)
            }
        })
    }

    override fun armyAppointList(gid: String) {
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armyAppointList(params), object : NetRequestListener<BaseBean<ArmyMember?>?> {
            override fun onSuccess(result: BaseBean<ArmyMember?>?) {
                getView()?.hideLoading()
                page++
                getView()?.handleArmyAppointList(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleArmyAppointList(null)
                getView()?.onError(e)
            }
        })
    }

    override fun armyAppointIndex(gid: String, level: String) {
        val params = ParamsMapUtils.commonParams
        params["uid"] = Biz.instance.uid()
        params["gid"] = gid
        params["level"] = level
        requestNetwork(apiService.armyAppointIndex(params), object : NetRequestListener<BaseBean<ArmyCurrentMember?>?> {
            override fun onSuccess(result: BaseBean<ArmyCurrentMember?>?) {
                getView()?.hideLoading()
                getView()?.handleArmyAppointIndex(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleArmyAppointIndex(null)
                getView()?.onError(e)
            }
        })
    }

    override fun armyAppoint(gid: String, level: String, toUid: String) {
        val params = ParamsMapUtils.commonParams
        params["uid"] = Biz.instance.uid()
        params["gid"] = gid
        params["level"] = level
        params["to_uid"] = toUid
        requestNetwork(apiService.armyAppoint(params), object : NetRequestListener<BaseBean<Any?>?> {
            override fun onSuccess(result: BaseBean<Any?>?) {
                getView()?.hideLoading()
                getView()?.handleArmyAppoint(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleArmyAppoint(null)
                getView()?.onError(e)
            }
        })
    }


}